package tq.com.tqteacher.adapt.newPublish;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;

import tq.com.tqcom.net.TQPictureUpload;
import tq.com.tqteacher.R;
import tq.com.tqteacher.adapt.viewHold.TQDonothingViewHold;
import tq.com.tqteacher.model.TQAtachModel;


public class TQVideoSetsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private ArrayList<TQAtachModel> data = new ArrayList<>();

    private boolean canModify = true;
    public TQVideoSetsAdapter(Context pContext){
        mContext = pContext;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup pViewGroup, int pI) {
        if(pI == 1){
            View lView = LayoutInflater.from(mContext).inflate(R.layout.vh_video_header0_layout, pViewGroup, false);
            return new TQDonothingViewHold(lView);
        }else{
            View lView = LayoutInflater.from(mContext).inflate(R.layout.vh_video_item_layout, pViewGroup, false);
            return new VideoItemViewHold(lView);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder pViewHolder, int pI) {
        if(pViewHolder instanceof VideoItemViewHold){
            TQAtachModel item = data.get(pI );
            ((VideoItemViewHold)pViewHolder).bindData(item);
        }
    }

    public void setCanModify(boolean pModify){
        canModify = pModify;
    }
    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public int getItemViewType(int position) {
//        if(position == 0) return 1;
        return 0;
    }

    public void repalceData(ArrayList<TQAtachModel> pData){
        data.clear();
        data.addAll(pData);
        notifyDataSetChanged();
    }

    public void update(TQAtachModel item){
        int pos = data.indexOf(item);
        if(pos != -1){
            notifyItemChanged(pos);
        }
    }

    public class VideoItemViewHold extends RecyclerView.ViewHolder{
        private ImageButton flag;
        private ImageButton play;

        private ImageView contentView;
        private TQAtachModel mTQVideoSetsModel;

        public VideoItemViewHold(View pView){
            super(pView);
            flag = pView.findViewById(R.id.flag);
            play = pView.findViewById(R.id.play);
            contentView = pView.findViewById(R.id.video);

            flag.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mItemClickListener != null && mTQVideoSetsModel !=null){
                        mItemClickListener.clickCapture(mTQVideoSetsModel);
                    }
                }
            });

            play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mItemClickListener != null && mTQVideoSetsModel !=null){
                        mItemClickListener.clickPlay(mTQVideoSetsModel);
                    }
                }
            });
        }

        public void bindData(TQAtachModel pTQVideoSetsModel){
            if(pTQVideoSetsModel != null){
                mTQVideoSetsModel = pTQVideoSetsModel;

                if(pTQVideoSetsModel.hasFile()){
                    play.setVisibility(View.VISIBLE);
                    flag.setImageResource(R.drawable.btn_recaption);
                }else{
                    play.setVisibility(View.INVISIBLE);
                    flag.setImageResource(R.drawable.bt_nocapture);
                }

                flag.setVisibility(canModify ? View.VISIBLE : View.INVISIBLE);

                String url = TQPictureUpload.downloadPath(pTQVideoSetsModel.getCover());
                RequestOptions lOptions = new RequestOptions().placeholder(R.drawable.ic_default_picture)
                        .error(R.drawable.ic_default_picture).diskCacheStrategy(DiskCacheStrategy.RESOURCE);

                Glide.with(mContext).load(url).apply(lOptions).into(contentView);
            }

        }
    }

    private IItemClickListener mItemClickListener;
    public void setItemClickListener(IItemClickListener pListener){
        mItemClickListener = pListener;
    }
    public interface IItemClickListener{
        void clickCapture(TQAtachModel videoItem);
        void clickPlay(TQAtachModel videoItem);
    }

}
